import axios from 'axios'
import { ElMessage } from 'element-plus';
import { storeA } from '@/piniaStore/storeA'


async function getuser(token:string,isflash:boolean){

return new Promise((resolve, reject)=>{
    var config = {
        method: 'get',
        url: 'http://wm.tocode.store/user/info',
        headers: { 
           'token': token?token:JSON.parse(localStorage.getItem("Usertoken")as string)
           ,
        }
     };
    axios(config)
.then(function (response) {
   console.log(response.data);
   if(isflash){
      const piniaStore = storeA()
       piniaStore.user.shopName = response.data.shopnumber
      piniaStore.user.Username = response.data.username
      piniaStore.user.token = response.data.token
      piniaStore.user.Address = response.data.address
   if(piniaStore.user.shopName){
      piniaStore.isHost = true
      console.log("是商家")
    }
   }
  
  
  resolve(response.data)

})
.catch(function (error) {
   console.log(error);
   reject()
});
})

}

function getlist(pageNum:string){
    return new Promise((resolve, reject)=>{
        var config = {
            method: 'get',
            url: 'http://wm.tocode.store/cai/all?pageNum='+pageNum+'&size=9',
            headers: { 
            
            }
         };
         
         axios(config)
         .then(function (response) {
           resolve(response.data)
         })
         .catch(function (error) {
            console.log(error);
            ElMessage({
                message: '出现错误！这肯定是后端的锅！',
                type: 'error',
              })
            reject()

         });
    })
    
}
function getshoplist(name:string,token:string){
    return new Promise((resolve, reject)=>{
        var config = {
            method: 'get',
            url: 'http://wm.tocode.store/shop/allCai?shopname='+name+'&page=1&size=99',
            headers: { 
               'token': token, 
            }
         };
         axios(config)
         .then(function (response) {
            console.log(response.data);
            resolve(response.data.list)
         })
         .catch(function (error) {
            console.log(error);
            reject()
         });
    })
}
export{
    getuser,getlist,getshoplist
}